package cn.wolfcode.mp.web.controller;


import cn.wolfcode.mp.anno.RequiredPermission;
import cn.wolfcode.mp.domain.Department;
import cn.wolfcode.mp.query.DepartmentQuery;
import cn.wolfcode.mp.service.IDepartmentService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    //处理查询所有部门请求
    // /department/list 目前不带参数,响应HTML 格式内容
    @RequiredPermission(name="部门页面",expression="department:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") DepartmentQuery qo) {
        //获取所有 部门数据,存到Model 中
        IPage<Department> page = departmentService.queryPage(qo);
        model.addAttribute("page", page);
        return "department/list";
    }

    // 访问路径 /department/delete?id=id值
    // 处理查询所有部门删除的请求,浏览器重新发送一条请求去查询一次
    @RequiredPermission(name="部门删除",expression="department:delete")
    @ResponseBody
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
            departmentService.removeById(id);
        }
        return "redirect:/department/list"; //让浏览器再发一条请求去查询
        //return JsonResult.success();
    }

    //处理保存的请求 /department/saveOrUpdate 带上参数
    @RequiredPermission(name="部门新增/编辑",expression="department:saveOrUpdate")
    @ResponseBody
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Department department) {
        if (department.getId() != null) {
            departmentService.saveOrUpdate(department);
        }
        return "redirect:/department/list"; //重定向
       // return JsonResult.success();
    }
}
